home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12046 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: coranto.ucs.mun.ca!usenet
  2. From: saustin@terra.nlnet.nf.ca (Steve Austin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Can you overload a constructor?
  5. Date: Mon, 18 Mar 1996 00:44:16 GMT
  6. Organization: Kickham Productions
  7. Message-ID: <4iibka$6rp@coranto.ucs.mun.ca>
  8. References: <Do859K.K9B@watserv3.uwaterloo.ca> <31483CDC.4B0C@staff.ichange.com> <4ibqan$br5@si-nic.hrz.uni-siegen.de> <4ic92p$lj6@B1FF.mindspring.com>
  9. NNTP-Posting-Host: n104h130.nlnet.nf.ca
  10. X-Newsreader: Forte Agent .99b.112
  11.  
  12. rudd@mindspring.com (Justin Rudd) rightly points out that constructors
  13. can be overloaded...but then adds...
  14.  
  15. >...Of course they do get called but not always.
  16.  
  17. They certainly do if an instance of the class is being created.
  18.  
  19. >...Now as for constructors always being called.  What if I have this:
  20. >
  21. >class Graphic
  22. >{
  23. >    public:
  24. >        virtual void Draw() = 0;
  25. >};
  26. >
  27. >class Box : public Graphic
  28. >{
  29. >    public:
  30. >        virtual void Draw();
  31. >};
  32. >
  33. >void main()
  34. >{
  35. >    Graphic* graph;        
  36. >    Box box;
  37. >
  38. >    graph = &box;
  39. >
  40. >    graph->Draw();
  41. >}
  42. >    
  43. >In this little code segment here I have a POINTER to a Graphic object.
  44. >It has not been initialized..therefore no constructor call.  And then
  45. >after that...I make the Graphic pointer point to a Box object...so at
  46. >no time was the Graphic pointer an instance of Graphic.
  47.  
  48. I'm not sure what point you're making. Constructors are only called
  49. when an instance of a class is being created. Declaring a pointer to a
  50. base class obviously is not creating an instance of the class. But
  51. when you create an instance of Box, the base Graphic constructor will
  52. be called, followed by the derived Box constructor.
  53.  
  54. There's no way around it, when you instantiate a class, the
  55. constructor will always be called. How else would the object be
  56. created?
  57.  
  58. - Steve
  59.  
  60.